﻿using Architecture.Domain.Services.Interface;
using Architecture.Domain.TraceabilityManage;
using Architecture.Domian.DTO;
using Architecture.Repository.Impl;
using Architecture.Repository.Interface;
using System.Security.Cryptography;

namespace Architecture.Domain.Services.Impl
{
    public class SlaughterService : ISlaughterService
    {

        private readonly ISlaughterRepository _slaughterRepository;
        private readonly ICorporationRepository _corporationRepository;

        public SlaughterService(
            ISlaughterRepository slaughterRepository,
            ICorporationRepository corporationRepository)
        {
            _slaughterRepository = slaughterRepository;
            _corporationRepository = corporationRepository;
        }

        public async Task<int> AddSlaughter(Slaughter slaughter)
        {
            return await _slaughterRepository.AddAsync(slaughter);
        }

        public async Task<Slaughter> GetSlaughterInfo(string? productionLot)
        {
            return await _slaughterRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);
        }

        public async Task<int> UpdateSlaughter(Slaughter slaughter)
        {
            return await _slaughterRepository.UpdateAsync(slaughter);
        }

        public async Task<int> BatchSlaughterPicList(string? productionLot, string? urlImg)
        {
            var slaughter = await _slaughterRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

            if (slaughter == null)
            {
                Slaughter slaughter1 = new()
                {
                    ProductionLot = productionLot,
                    RelevantQualification = urlImg,
                };
                return await AddSlaughter(slaughter1);
            }
            else
            {
                slaughter.RelevantQualification = urlImg;

                return await _slaughterRepository.UpdateAsync(slaughter);
            }
        }

        public async Task<List<PictureDTO>> GetSlaughterPicture(string? productionLot)
        {
            var slaughter = await _slaughterRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

            if (slaughter == null || string.IsNullOrEmpty(slaughter.RelevantQualification))
            {
                return new List<PictureDTO>();
            }

            return slaughter.RelevantQualification.Split(',').Select(x => new PictureDTO
            {
                name = x,
                url = "http://localhost:5124/Images/" + x
            }).ToList();
        }

        public async Task<List<PictureDTO>> GetCorporationPicture(string? productionLot, int slaughteringCompanyId)
        {
            var corporation = await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete && x.CId == slaughteringCompanyId);

            if (corporation == null || string.IsNullOrEmpty(corporation.CompanyPicture))
            {
                return new List<PictureDTO>();
            }

            var result = corporation.CompanyPicture.Split(',').Select(x => new PictureDTO
            {
                name = x,
                url = "http://localhost:5124/Images/" + x
            }).ToList();

            return result;
        }

        public async Task<int> BatchCorporationPicList(string? productionLot, string? urlImg, int sId)
        {
            var corporation = await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

            if (corporation == null)
            {
                await AddCorporation(new Corporation { ProductionLot = productionLot });

                corporation = await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

                if (sId == 0)
                {
                    await AddSlaughter(new Slaughter { ProductionLot = productionLot, SlaughteringCompanyId = corporation.CId });
                }

                corporation.CompanyPicture = urlImg;
                return await _corporationRepository.UpdateAsync(corporation);
            }
            else
            {
                corporation.CompanyPicture = urlImg;
                return await _corporationRepository.UpdateAsync(corporation);
            }
        }

        public async Task<int> AddCorporation(Corporation corporation)
        {
            var slaughter = await _slaughterRepository.GetByFuncAsync(x => x.ProductionLot == corporation.ProductionLot && !x.IsDelete);

            if (slaughter == null)
            {
                await _corporationRepository.AddAsync(corporation);

                Slaughter slaughter1 = new()
                {
                    SlaughteringCompanyId = corporation.CId,
                    ProductionLot = corporation.ProductionLot
                };
                return await AddSlaughter(slaughter1);
            }

            await _corporationRepository.AddAsync(corporation);

            slaughter.SlaughteringCompanyId = corporation.CId;

            return await _slaughterRepository.UpdateAsync(slaughter);
        }

        public async Task<Corporation> GetCorporationInfo(string? productionLot, int slaughteringCompanyId)
        {
            var result = await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete && x.CId == slaughteringCompanyId);
            return result;
        }

        public async Task<int> UpdateCorporation(Corporation corporation)
        {
            var slaughter = await _slaughterRepository.GetByFuncAsync(x => x.ProductionLot == corporation.ProductionLot && !x.IsDelete);

            if (slaughter == null)
            {
                Slaughter slaughter1 = new()
                {
                    SlaughteringCompanyId = corporation.CId,
                    ProductionLot = corporation.ProductionLot
                };

                return await AddSlaughter(slaughter1);
            }

            slaughter.SlaughteringCompanyId = corporation.CId;

            await UpdateSlaughter(slaughter);

            return await _corporationRepository.UpdateAsync(corporation);
        }
    }
}
